/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @author 品讯科技
 */

package com.px.pa.modulars.circle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.px.basic.alone.security.annotation.Inner;
import com.px.basic.alone.security.util.SecurityUtils;
import com.px.pa.modulars.circle.entity.CircleLogin;
import com.px.pa.modulars.circle.service.CircleLoginService;
import com.px.pa.modulars.upms.entity.SysUser;
import com.px.pa.modulars.upms.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author 品讯科技
 * @date 2024-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/circlelogin" )
@Api(value = "circlelogin", tags = "管理")
public class CircleLoginController {

    private final  CircleLoginService circleLoginService;

    @Autowired
    private SysUserService sysUserService;
    private SysUser getUser(){
        System.out.println(SecurityUtils.getUser());
        SysUser user = sysUserService.getById(SecurityUtils.getUser().getId());
        return user;
    }
    /**
     * 分页查询
     * @param page 分页对象
     * @param circleLogin 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
   // @PreAuthorize("@pms.hasPermission('circleLogo_circlelogin_get')" )
    @Inner(value = false)
    public R getCircleLoginPage(Page page, CircleLogin circleLogin) {
        return R.ok(circleLoginService.page(page, Wrappers.query(circleLogin)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
   // @PreAuthorize("@pms.hasPermission('circleLogo_circlelogin_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(circleLoginService.getById(id));
    }

    /**
     * 新增
     * @param circleLogin 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping
    //@PreAuthorize("@pms.hasPermission('circleLogo_circlelogin_add')" )
    public R save(@RequestBody CircleLogin circleLogin) {
        circleLogin.setTenantCode(getUser().getOnlyLogo());
        return R.ok(circleLoginService.save(circleLogin));
    }

    /**
     * 修改
     * @param circleLogin 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping
  //  @PreAuthorize("@pms.hasPermission('circleLogo_circlelogin_edit')" )
    public R updateById(@RequestBody CircleLogin circleLogin) {
        return R.ok(circleLoginService.saveOrUpdate(circleLogin));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/{id}" )
   // @PreAuthorize("@pms.hasPermission('circleLogo_circlelogin_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(circleLoginService.removeById(id));
    }


    /**
     * 通过id查询

     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/get" )
    public R getById() {
        String tenant = getUser().getOnlyLogo();
        CircleLogin login = circleLoginService.getOne(new QueryWrapper<CircleLogin>().eq("tenant_code",tenant));
        return R.ok(login);
    }

}
